package com.party.mobile.web.controller.crowdfund;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.party.authorization.annotation.Authorization;
import com.party.common.constant.Constant;
import com.party.common.paging.Page;
import com.party.common.utils.*;
import com.party.core.exception.BusinessException;
import com.party.core.model.BaseModel;
import com.party.core.model.YesNoStatus;
import com.party.core.model.activity.Activity;
import com.party.core.model.activity.ActivityInfo;
import com.party.core.model.activity.ActivityType;
import com.party.core.model.copartner.TargetCopartner;
import com.party.core.model.crowdfund.CrowdfundEvent;
import com.party.core.model.crowdfund.TargetProject;
import com.party.core.model.distributor.DistributorCount;
import com.party.core.model.distributor.DistributorRelation;
import com.party.core.model.distributor.DistributorRelationType;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.system.TargetType;
import com.party.core.service.activity.IActivityInfoService;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.copartner.IBrokerageItemService;
import com.party.core.service.copartner.ITargetCopartnerService;
import com.party.core.service.crowdfund.ICrowdfundEventService;
import com.party.core.service.crowdfund.ITargetProjectService;
import com.party.core.service.distributor.IDistributorCountService;
import com.party.core.service.distributor.IDistributorRelationService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberMerchantService;
import com.party.core.service.wechatPlatform.IWechatMerchantService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import com.party.mobile.biz.activity.ActivityBizService;
import com.party.mobile.biz.analysis.StatisticsUserBizService;
import com.party.mobile.biz.crowdfund.CrowdfundActivityBizService;
import com.party.mobile.biz.crowdfund.TargetBizService;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.core.utils.MyBeanUtils;
import com.party.mobile.web.annotation.VisitCollect;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.activity.input.EditActivityInput;
import com.party.mobile.web.dto.copartner.output.GetDateOutput;
import com.party.mobile.web.dto.copartner.output.ProjectCountOutput;
import com.party.mobile.web.dto.copartner.output.RepresentProjectListOutput;
import com.party.mobile.web.dto.crowdfund.input.TargetHomeInput;
import com.party.mobile.web.dto.crowdfund.output.ActivityDetailOutput;
import com.party.mobile.web.dto.crowdfund.output.DistActivityDetailOutput;
import com.party.mobile.web.dto.crowdfund.output.TargetHomeOutput;
import com.party.mobile.web.dto.crowdfund.output.TargetOutput;
import com.party.mobile.web.dto.login.output.CurrentUser;
import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.OperatingSystem;
import nl.bitwalker.useragentutils.UserAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 众筹目标控制器
 * Created by wei.li
 *
 * @date 2017/3/1 0001
 * @time 10:17
 */

@Controller
@RequestMapping(value = "/crowdfund/target")
public class TargetController {

    @Autowired
    private TargetBizService targetBizService;

    @Autowired
    private ITargetProjectService targetProjectService;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IMemberMerchantService memberMerchantService;

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private CurrentUserBizService currentUserBizService;

    @Autowired
    private ActivityBizService activityBizService;

    @Autowired
    private CrowdfundActivityBizService crowdfundActivityBizService;

    @Autowired
    private ITargetCopartnerService targetCopartnerService;

    @Autowired
    private IWechatMerchantService wechatMerchantService;

    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;

    @Autowired
    private StatisticsUserBizService statisticsUserBizService;

    @Autowired
    private ICrowdfundEventService crowdfundEventService;

    @Autowired
    private IDistributorRelationService distributorRelationService;

    @Autowired
    private IDistributorCountService distributorCountService;

    @Autowired
    private IActivityInfoService activityInfoService;

    @Autowired
    private IBrokerageItemService brokerageItemService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 获取当前用户的众筹目标
     * @param page 分页参数
     * @param status 状态(0:进行中, 1:已经截止)
     * @param id 发布者编号
     * @param request 请求参数
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "list")
    @VisitCollect(module = TargetType.ZC_ACTIVITY)
    public AjaxResult list(Page page, Integer status, String id, HttpServletRequest request) {
        try {
            List<TargetOutput> targetOutputList = targetBizService.list(page, status, id);
            return AjaxResult.success(targetOutputList, page);
        } catch (Exception e) {
            return AjaxResult.error(100, "系统异常");
        }
    }

    /**
     * 主页显示的众筹项目
     * @param page 分页参数
     * @return 交互参数
     */
    @ResponseBody
    @RequestMapping(value = "home/list")
    public AjaxResult homeList(Page page, TargetHomeInput input) {
        try {
            List<TargetHomeOutput> list = targetBizService.homeList(page, input);
            return AjaxResult.success(list, page);
        } catch (Exception e) {
            return AjaxResult.error(100, "系统异常");
        }
    }

    /**
     * 根据项目ID或者众筹ID获取发布者商户信息
     *
     * @param projectId 众筹id
     * @param targetId  项目id
     * @param eventId   主题id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getTargetByProject")
    public AjaxResult getTargetByProject(String projectId, String targetId, String eventId) {
        //参数验证
        if (Strings.isNullOrEmpty(projectId) && Strings.isNullOrEmpty(targetId) && Strings.isNullOrEmpty(eventId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "projectId和targetId和eventId不能同时为空");
        }
        try {
            Map<String, Object> data = new HashMap<String, Object>();
            String memberId = null;
            if (StringUtils.isNotEmpty(projectId)) {
                TargetProject targetProject = targetProjectService.findByProjectId(projectId);
                if(null == targetProject || targetProject.getDelFlag().equals("1")){
                    return AjaxResult.error(PartyCode.IS_NO_DATA, "该项目不存在");
                }
                if (targetProject.getType().equals(Constant.CROWD_FUND_TYPE_ACTIVITY)) {
                    Activity activity = activityService.get(targetProject.getTargetId());
                    memberId = activity.getMember();
                }
            } else if (StringUtils.isNotEmpty(targetId)) {
                Activity activity = activityService.get(targetId);
                if(null == activity ){
                    return AjaxResult.error(PartyCode.IS_NO_DATA, "活动不存在");
                }
                memberId = activity.getMember();
            } else if (StringUtils.isNotEmpty(eventId)) {
                CrowdfundEvent crowdfundEvent = crowdfundEventService.get(eventId);
                if (null == crowdfundEvent) {
                    return AjaxResult.error(PartyCode.IS_NO_DATA, "主题不存在");
                }
                memberId = crowdfundEvent.getCreateBy();
            }

            if (StringUtils.isNotEmpty(memberId)) {
                String authorizerAppid = wechatMerchantService.getAppid(memberId);
                if (StringUtils.isNotEmpty(authorizerAppid)) {
                    data.put("appId", authorizerAppid);
                } else {
                    data.put("appId", wechatOpenMemberService.getSystem().getAuthorizerAppid());
                }
                data.put("memberId", memberId);
            } else {
                logger.error("数据异常：getTargetByProject接口报错：projectId——" + projectId + "；targetId——" + targetId);
                return AjaxResult.error();
            }
            return AjaxResult.success(data);
        } catch (Exception e) {
            logger.error("getTargetByProject接口报错：projectId——" + projectId + "；targetId——" + targetId, e);
            return AjaxResult.error();
        }
    }

    /**
     * 根据memberId获取商户信息
     *
     * @param memberId 机构id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAppIdByMember")
    public AjaxResult getAppIdByMember(String memberId) {
        //参数验证
        if (Strings.isNullOrEmpty(memberId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "memberId不能为空");
        }

        try {
            String authorizerAppid = wechatMerchantService.getAppid(memberId);
            if (null != authorizerAppid) {
                return AjaxResult.success((Object) authorizerAppid);
            } else {
                return AjaxResult.success((Object) wechatOpenMemberService.getSystem().getAuthorizerAppid());
            }
        } catch (Exception e) {
            logger.error("getAppIdByMember接口异常：memberId——" + memberId, e);
            return AjaxResult.error();
        }
    }

    @ResponseBody
    @RequestMapping("/getTargetCreator")
    public AjaxResult getTargetCreator(String id) {
        try {
            MemberGroup member = memberGroupService.get(id);
            Map<String,Object> map = Maps.newHashMap();
            map.put("authorName",member.getRealname());
            return AjaxResult.success(map);
        } catch (BusinessException be) {
            return AjaxResult.error(be.getCode(), be.getMessage());
        }
    }

    /**
     * 合伙人可参与众筹项目列表
     *
     * @param request
     * @param page
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping("fxActivityList")
    public AjaxResult fxActivityList(HttpServletRequest request, Page page, Integer isJoin) {
        CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
        Map<String, Object> params = Maps.newHashMap();
        params.put("partnerId", currentUser.getId());
        params.put("delFlag", BaseModel.DEL_FLAG_NORMAL);
        if (null != isJoin && isJoin == 1) {
            params.put("isJoin", 1);
        }
        List<Map<String, Object>> activities = activityService.copartnerListPage(params, page);
        for (Map<String, Object> result : activities) {
            Date endTime = (Date) result.get("endTime");
            Integer inProgress = activityBizService.setIsInProgress(endTime);
            result.put("inProgress", inProgress);
        }
        return AjaxResult.success(activities, page);
    }

    /**
     * 合伙人众筹项目详情
     *
     * @param id 项目id
     * @return
     */
    @ResponseBody
    @RequestMapping("fxActivityDetail")
    public AjaxResult fxActivityDetail(String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "众筹项目编号不能为空");
        }
        try {
            ActivityDetailOutput activityDetailOutput = crowdfundActivityBizService.detail(id, null, null);
            DistActivityDetailOutput output = new DistActivityDetailOutput();
            MyBeanUtils.copyBeanNotNull2Bean(activityDetailOutput, output);

            TargetCopartner targetCopartner = targetCopartnerService.findByTargetId(id);
            output.setOrderNum(targetCopartner.getOrderNum());
            output.setTotalMoney(targetCopartner.getTotalMoney());
            output.setTotalBrokerage(targetCopartner.getTotalBrokerage());
            output.setIsJoin(targetCopartner.getIsJoin());
            return AjaxResult.success(output);
        } catch (Exception e) {
            return AjaxResult.error(999, "项目详情异常");
        }
    }

    /**
     * 合伙人确定参加
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("ensureJoin")
    public AjaxResult ensureJoin(HttpServletRequest request, String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "众筹项目编号不能为空");
        }
        CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
        String partnerId = currentUser.getId();
        Integer r = activityBizService.ensureJoin(id, partnerId);
        if (1 == r) {
            return AjaxResult.success();
        } else if (r > 1) {
            return AjaxResult.success("参与推广成功, 但是修改了多条数据, 请注意!");
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 获取合伙人活动详情的数据页
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("activityData")
    public AjaxResult activityData(String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "众筹项目编号不能为空");
        }
        Map<String, Object> map = activityBizService.activityData(id);
        return AjaxResult.success(map);
    }

    /**
     * 获取合伙人编辑页面的数据
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("getEditActivity")
    public AjaxResult getEditActivity(String id) {
        Activity activity = activityService.get(id);
        EditActivityInput editActivityInput = new EditActivityInput();
        BeanUtils.copyProperties(activity, editActivityInput);
        return AjaxResult.success(editActivityInput);
    }

    /**
     * 合伙人编辑
     * @param editActivityInput
     * @return
     */
    @ResponseBody
    @RequestMapping("editActivity")
    public AjaxResult editActivity(EditActivityInput editActivityInput) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(editActivityInput, activity);
        boolean r = activityService.update(activity);
        if (r) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 合作渠道众筹活动列表
     *
     * @param request
     * @param page    分页参数
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping("activityForCosponsorList")
    public AjaxResult activityForCosponsorList(HttpServletRequest request, Page page) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            Map<String, Object> params = Maps.newHashMap();
            params.put("cosponsorId", currentUser.getId());
            List<Map<String, Object>> listOutputs = activityService.cosponsorList(params, page);
            for (Map<String, Object> result : listOutputs) {
                Date endTime = (Date) result.get("endTime");
                Integer inProgress = activityBizService.setIsInProgress(endTime);
                result.put("inProgress", inProgress);
            }
            return AjaxResult.success(listOutputs, page);
        } catch (Exception e) {
            return AjaxResult.error(100, "获取合作渠道众筹活动列表异常");
        }
    }

    /**
     * 合作渠道众筹活动数据
     *
     * @param activityId 活动id
     * @return
     */
    @ResponseBody
    @RequestMapping("activityDataForCosponsor")
    public AjaxResult activityDataForCosponsor(String activityId) {
        if (StringUtils.isEmpty(activityId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "活动id不能为空");
        }
        try {
            Activity activity = activityService.get(activityId);
            GetDateOutput getDateOutput = new GetDateOutput();
            getDateOutput.setSuccessNum(activity.getCrowdfundedNum());
            getDateOutput.setAmount(activity.getTotalMoney().floatValue());
            getDateOutput.setCreateDate(activity.getStartTime());
            getDateOutput.setApplyNum(activity.getJoinNum());
            getDateOutput.setEndTime(activity.getEndTime());
            getDateOutput.setOrderNum(activity.getOrderNum().intValue());
            return AjaxResult.success(getDateOutput);
        } catch (Exception e) {
            return AjaxResult.error(100, "获取项目数据异常");
        }
    }

    /**
     * 获取众筹活动数据
     *
     * @param activityId 活动id
     * @return
     */
    @ResponseBody
    @RequestMapping("activityCount")
    public AjaxResult activityCount(String activityId) {
        if (StringUtils.isEmpty(activityId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "活动id不能为空");
        }

        try {
            Activity activity = activityService.get(activityId);
            ProjectCountOutput output = new ProjectCountOutput();
            output.setApplyNum(activity.getJoinNum());
            output.setSuccessNum(activity.getCrowdfundedNum());
            return AjaxResult.success(output);
        } catch (Exception e) {
            logger.error("获取众筹活动数据异常", e);
            return AjaxResult.error(100, "获取众筹活动数据异常");
        }
    }

    /**
     * 合作渠道项目的众筹列表
     *
     * @param isSuccess  是否成功 1:是 2：不传默认是全部
     * @param activityId 活动编号
     * @param page       分页参数
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping("projectForActivityList")
    public AjaxResult projectForActivityList(Integer isSuccess, String activityId, Page page) {
        if (StringUtils.isEmpty(activityId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "活动id不能为空");
        }

        try {
            List<RepresentProjectListOutput> list = targetBizService.projectForActivityList(isSuccess, activityId, page);
            return AjaxResult.success(list, page);
        } catch (Exception e) {
            logger.error("查询众筹列表异常", e);
            return AjaxResult.error(100, "查询众筹列表异常");
        }
    }

    /**
     * 合作渠道项目的众筹列表
     *
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping("test")
    public AjaxResult projectForActivityList(HttpServletRequest request) {
        try {
            Map<String,String> mmp = new HashMap<>();
            String agent = request.getHeader("User-Agent");
            UserAgent userAgent = UserAgent.parseUserAgentString(agent);
            OperatingSystem operatingSystem = userAgent.getOperatingSystem();
            Browser browser = userAgent.getBrowser();
            //系统名
            String system = operatingSystem.getName();
            //ip地址
            String ipAddress = HttpServletRequestUtils.getRemoteIpAddress(request);
            //浏览器名
            String browserName= browser.getName();
            //设备类型
            String deviceType = operatingSystem.getDeviceType()!=null ?operatingSystem.getDeviceType().getName():"";
            //生成userID
            String uid = URLEncoder.encode(AESUtil.encrypt("userId",system+ipAddress+browserName+deviceType), "utf-8");
            mmp.put("system",system);
            mmp.put("ipAddress",ipAddress);
            mmp.put("browserName",browserName);
            mmp.put("deviceType",deviceType);
            mmp.put("userIdc",AESUtil.decrypt("userId",uid));
         return AjaxResult.success(mmp);
        } catch (Exception e) {
            logger.error("查询众筹列表异常", e);
            return AjaxResult.error(100, "查询众筹列表异常");
        }
    }

    /**
     * 合伙人可参与众筹项目列表
     *
     * @param page         分页参数
     * @param isJoin       是否参与 1：已参与 0：全部
     * @param activityType 活动类型 4：众筹活动 5：课程活动
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping("newFxActivityList")
    public AjaxResult newFxActivityList(Page page, Integer isJoin, Integer activityType, HttpServletRequest request) {
        try {
            if (null == activityType) {
                activityType = ActivityType.CROWDFUND.getCode();
            }
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            Map<String, Object> params = Maps.newHashMap();
            params.put("distributorId", currentUser.getId());
            if (ActivityType.CROWDFUND.getCode().equals(activityType)) {
                params.put("isCrowdfunded", 1);
                params.put("distributionType", DistributorRelationType.CROWDFUND_DISTRIBUTION.getCode());
            } else if (ActivityType.COURSE.getCode().equals(activityType)) {
                params.put("activityType", ActivityType.COURSE.getValue());
                params.put("distributionType", DistributorRelationType.COURSE_DISTRIBUTION.getCode());
            }
            if (null != isJoin && isJoin == 1) {
                params.put("isJoin", 1);
            }
            List<Map<String, Object>> activities = activityService.copartnerListPage2(params, page);
            for (Map<String, Object> result : activities) {
                Date endTime = (Date) result.get("endTime");
                Integer inProgress = activityBizService.setIsInProgress(endTime);
                result.put("inProgress", inProgress);
                result.put("activityType", activityType);
            }
            return AjaxResult.success(activities, page);
        } catch (Exception e) {
            logger.error("合伙人已/可参与众筹项目列表异常", e);
            return AjaxResult.error(100, "合伙人已/可参与众筹项目列表异常");
        }
    }

    /**
     * 获取合伙人活动详情的数据页
     *
     * @param id           活动id
     * @param request
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping("newActivityData")
    public AjaxResult newActivityData(String id, HttpServletRequest request) {
        try {
            if (StringUtils.isEmpty(id)) {
                return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "众筹项目编号不能为空");
            }
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            String memberId = currentUser.getId();

            Activity activity = activityService.get(id);
            ActivityInfo activityInfo = activityInfoService.findByActivityId(id);
            if (null == activityInfo) {
                return AjaxResult.error(110, "该活动没有配置佣金");
            }

            Integer activityType = activityService.getActivityType(activity);

            Integer distributionType = null;
            if (ActivityType.CROWDFUND.getCode().equals(activityType)) {
                distributionType = DistributorRelationType.CROWDFUND_DISTRIBUTION.getCode();
            } else if (ActivityType.COURSE.getCode().equals(activityType)) {
                distributionType = DistributorRelationType.COURSE_DISTRIBUTION.getCode();
            }

            DistributorRelation relation = distributorRelationService.get(distributionType, id, memberId);
            if (null == relation) {
                return AjaxResult.error(PartyCode.IS_NO_DATA, "分销关系不存在");
            }

            DistributorCount distributorCount = distributorCountService.findByRelationId(relation.getId());

            Double totalBrokerage = brokerageItemService.getDisBrokerage(memberId, id);

            Map<String, Object> resultMap = Maps.newHashMap();
            resultMap.put("activityId", id);
            resultMap.put("copartnerId", memberId);
            resultMap.put("endTime", activity.getEndTime());
            resultMap.put("joinTime", relation.getCreateDate());
            if (ActivityType.CROWDFUND.getCode().equals(activityType)) {
                resultMap.put("joinNum", distributorCount.getCrowdfundNum()); // 报名人员
                resultMap.put("crowdfundedNum", distributorCount.getSuccessNum()); // 众筹成功
                resultMap.put("orderNum", distributorCount.getFavorerNum()); // 订单数量
            } else if (ActivityType.COURSE.getCode().equals(activityType)) {
                resultMap.put("joinNum", distributorCount.getApplyNum()); // 报名人员
            }
            resultMap.put("totalBrokerage", BigDecimalUtils.round(totalBrokerage, 2)); // 累计佣金
            resultMap.put("totalMoney", BigDecimalUtils.round(distributorCount.getFavorerAmount(), 2)); // 累计资金
            resultMap.put("distributorRelationId", relation.getId()); // 分销关系id
            resultMap.put("parentId", relation.getParentId());
            resultMap.put("distributorId", relation.getDistributorId());
            resultMap.put("title", activity.getTitle());
            resultMap.put("remarks", activity.getRemarks());
            resultMap.put("activityType", activityType);

            return AjaxResult.success(resultMap);
        } catch (Exception e) {
            logger.error("合伙人众筹项目详情异常", e);
            return AjaxResult.error(100, "合伙人众筹项目详情异常");
        }
    }

    /**
     * 合伙人众筹项目详情
     *
     * @param id 项目id
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping("newFxActivityDetail")
    public AjaxResult newFxActivityDetail(String id, HttpServletRequest request) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "众筹项目编号不能为空");
        }
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            ActivityDetailOutput activityDetailOutput = crowdfundActivityBizService
                    .detail(id, currentUser, DistributorRelationType.CROWDFUND_DISTRIBUTION.getCode());
            DistActivityDetailOutput output = new DistActivityDetailOutput();
            MyBeanUtils.copyBeanNotNull2Bean(activityDetailOutput, output);

            if (StringUtils.isEmpty(activityDetailOutput.getParentId())) {
                output.setParentId(activityDetailOutput.getAuthorId());
            }

            DistributorRelation distributorRelation = distributorRelationService.
                    get(DistributorRelationType.CROWDFUND_DISTRIBUTION.getCode(), id, currentUser.getId());
            if (null != distributorRelation) {
                output.setIsJoin(YesNoStatus.YES.getCode());
            } else {
                output.setIsJoin(YesNoStatus.NO.getCode());
            }
            ActivityInfo activityInfo = activityInfoService.findByActivityId(id);
            if (null != activityInfo) {
                output.setBrokerage(activityInfo.getDirectBrokerage());
            }
            return AjaxResult.success(output);
        } catch (Exception e) {
            return AjaxResult.error(999, "项目详情异常");
        }
    }
}
